//
//  MyViewController.m
//  ToWhere
//
//  Created by dllo on 15/11/12.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "MyViewController.h"
#import "AppDelegate.h"
#import "YRSideViewController.h"
#import "MyTableViewCell.h"
#import "MyPoticViewController.h"
#import "MyTravelViewController.h"
#import "MyMusicViewController.h"



@interface MyViewController ()<UITableViewDataSource, UITableViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>

@property(nonatomic, retain) UITableView *tableView;

@property(nonatomic, retain) NSArray *imageArray;

@property(nonatomic, retain) NSArray *titleArray;

@property(nonatomic, retain) UIImageView *image;


@end

@implementation MyViewController

- (void)dealloc
{
    [_tableView release];
    [_imageArray release];
    [_titleArray release];
    [_image release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];

  

    self.view.backgroundColor = [UIColor clearColor];
    
    //初始化
    [self initData];
    
    //创建页面
    [self createView];
    
    //创建tableView
    [self createUitableView];
    
}

#pragma mark 初始化
-(void)initData
{
    self.imageArray = @[@"my_zhuanti", @"my_lvxing", @"my_youji"];
    self.titleArray = @[@"我的专题", @"我的游记", @"我的音旅"];
}

#pragma mark 创建页面
-(void)createView
{
    UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"myback"]];
    imageview.frame = self.view.frame;
    [self.view addSubview:imageview];
    [imageview release];
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    effectView.frame = imageview.bounds;
    effectView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.1];
    [imageview addSubview:effectView];
}

#pragma mark 创建tableView
-(void)createUitableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0 * kHeight, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.tableFooterView = [[[UIView alloc]initWithFrame:CGRectZero] autorelease];
    
    [self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"MyTableViewCell"];
    
    //左侧菜单
    UIButton *buttonView = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonView setBackgroundImage:[UIImage imageNamed:@"myMenu.png"] forState:UIControlStateNormal];
    buttonView.frame = CGRectMake(10 * kWidth, 30 * kHeight, kWidth * 30, kHeight * 30);
    [buttonView addTarget:self action:@selector(meumAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonView];
}

//左侧菜单
- (void)meumAction:(UIButton *)button{
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    YRSideViewController *sideViewController=[delegate yrsideView];
    [sideViewController showLeftViewController:true];
}

//返回行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.titleArray.count;
}

//返回分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//返回重用池
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyTableViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.imageName = self.imageArray[indexPath.row];
    cell.titleName = self.titleArray[indexPath.row];
    return cell;

}

//返回行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60 * kHeight;
}

//返回头视图高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 250 * kHeight;
}

//返回头视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * kHeight)];
    view.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0];
    
    //头像
    self.image = [[UIImageView alloc]init];
    NSData *data = [self readImageName];
    if (data != nil) {
        self.image.image = [UIImage imageWithData:data];
    }
    else{
        self.image.image = [UIImage imageNamed:@"2"];
    }
    self.image.frame = CGRectMake(self.view.frame.size.width / 2 - 40 * kWidth, 65 * kHeight, kWidth * 80, kWidth * 80);
    self.image.layer.masksToBounds = YES;
    self.image.layer.cornerRadius = kWidth * 80 / 2;
    [view addSubview:self.image];
    self.image.userInteractionEnabled=YES;
    UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(nameAction:)];
    [self.image addGestureRecognizer:singleTap];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(self.view.frame.size.width / 2 - 100 * kWidth, self.image.frame.origin.y + 120 * kHeight, 200 * kWidth, 40 * kHeight);
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [btn setTitle:@"趣 哪 儿" forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:17 * kWidth];
    [btn setTitleColor:[UIColor colorWithRed:1.000 green:0.499 blue:0.382 alpha:1.000] forState:UIControlStateNormal];
    [view addSubview:btn];
    [btn addTarget:self action:@selector(nameAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        MyPoticViewController *potic = [[MyPoticViewController alloc] init];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:potic animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    }else if (indexPath.row == 1){
    
        MyTravelViewController *travel = [[MyTravelViewController alloc] init];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:travel animated:YES];
        self.hidesBottomBarWhenPushed = NO;
    
    }else
    {
        MyMusicViewController *travel = [[MyMusicViewController alloc] init];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:travel animated:YES];
        self.hidesBottomBarWhenPushed = NO;

    }
    

}
////昵称点击事件
//-(void)nameAction
//{
//    [UIView animateWithDuration:1.0 animations:^{
//        
//        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10 * kWidth, self.view.frame.size.height - 75 * kHeight, self.view.frame.size.width - 20 * kWidth, 70 * kHeight)];
//        view.backgroundColor = [UIColor whiteColor];
//        
//        NSArray *array = @[@"更改昵称", @"取消"];
//        for (int i = 0; i < 2; i++) {
//            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//            btn
//        }
//        
//    }]
//}

//头像昵称点击事件
-(void)nameAction:(UIButton *)button
{
    [self UesrImageClicked];
}
- (void)UesrImageClicked
{
    UIActionSheet *sheet;
    
    // 判断是否支持相机
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        sheet  = [[UIActionSheet alloc]
                  initWithTitle:@"请选择"
                  delegate:self
                  cancelButtonTitle:@"取消"
                  destructiveButtonTitle:nil
                  otherButtonTitles:@"照相机",@"本地相册",nil];
    }
    else {
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"从相册选择" otherButtonTitles:@"取消", nil];
    }
    
    sheet.tag = 255;
    
    [sheet showInView:self.view];
    
}
#pragma mark - action sheet delegte
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 255) {
        NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            switch (buttonIndex) {
                case 0:
                    sourceType = UIImagePickerControllerSourceTypeCamera; //相机
                    break;
                case 1:
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //相册
                    break;
                case 2:
                    return;
            }
        }
        else {
            if (buttonIndex == 0) {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            } else {
                return;
            }
        }
        // 跳转到相机或相册页面
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType = sourceType;
        imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        
        [self presentViewController:imagePickerController animated:YES completion:^{}];
    }
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSLog(@"%@", info);
    NSData *imageData = UIImagePNGRepresentation([info objectForKey:@"UIImagePickerControllerEditedImage"]);
    NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *ImagePath = [Path stringByAppendingPathComponent:@"headerImage"];
    [imageData writeToFile:ImagePath atomically:NO];
    self.image.image = [UIImage imageWithData:imageData];
    [picker dismissViewControllerAnimated:YES completion:^{}];
}
// 取消按钮
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:^{}];
}
//读取数据
-(NSData *)readImageName
{
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    path = [path stringByAppendingPathComponent:@"headerImage"];
    return [NSData dataWithContentsOfFile:path];
}


-(void)viewWillDisappear:(BOOL)animated
{
   [[NSNotificationCenter defaultCenter] postNotificationName:@"抽屉关" object:self userInfo:nil];
}
-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"抽屉开" object:self userInfo:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
